package com.example.demo.entity.security;

import com.alibaba.fastjson.JSON;
import com.example.demo.entity.Result;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @description: 登录成功的返回
 * @author: cj
 * @create: 09-02-08 09:31:05
 */
@Component
public class LoginSuccessHandler implements AuthenticationSuccessHandler {


    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
        Result result = new Result();

        result.setStatus( 200 );
        result.setMessage( "登录成功" );
        result.setData( authentication.getPrincipal() );
        response.setCharacterEncoding( "UTF-8" );
        response.getWriter().write( JSON.toJSONString( result ) );
    }
}
